#include <iostream>
#include <iterator>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

// 下面这个版本是我自己写的，虽然代码有点多，但是能过oj
/*
string to_digits(string s, string target, string result)
{
    while(1)
    {
        int ret = s.find(target.c_str());
        if(ret > s.size())
            break;
        s.replace(ret, strlen(target.c_str()), result);
    }
    return s;
}

int main() 
{
    string str;
    getline(cin, str);
    string s1, s2;
    int flag = 0;
    for(auto& e : str)
    {
        if(e == '-')
        {
            flag = 1;
            continue;
        }
        if(flag == 0)
            s1 += e;
        else
            s2 += e;
    }

    // 任意一方是王炸
    if(s1 == "joker JOKER" || s2 == "joker JOKER")
    {
        cout << "joker JOKER" << endl;
        return 0;
    }

    string s1_tmp = s1;
    string s2_tmp = s2;

    // 一方是炸弹，一方不是
    if((s1.size() == 7 || s1.size() == 11) && (s2.size() != 7 && s2.size() != 11))
    {
        cout << s1 << endl;
        return 0;
    }
    else if((s2.size() == 7 || s2.size() == 11) && (s1.size() != 7 && s1.size() != 11))
    {
        cout << s2 << endl;
        return 0;
    }

    // 处理10，A，2等情况
    if(s1 == "10 10 10 10" || s1 == "10 10 10" || s1 == "10 10" || s1 == "10")
        s1 = to_digits(s1, "10", ":");

    if(s1 == "A A A A" || s1 == "A A A" || s1 == "A A" || s1 == "A")
        s1 = to_digits(s1, "A", "L");

    if(s1 == "2 2 2 2" || s1 == "2 2 2" || s1 == "2 2" || s1 == "2")
        s1 = to_digits(s1, "2", "M");

    if(s2 == "10 10 10 10" || s2 == "10 10 10" || s2 == "10 10" || s2 == "10")
        s2 = to_digits(s2, "10", ":");

    if(s2 == "A A A A" || s2 == "A A A" || s2 == "A A" || s2 == "A")
        s2 = to_digits(s2, "A", "L");

    if(s2 == "2 2 2 2" || s2 == "2 2 2" || s2 == "2 2" || s2 == "2")
        s2 = to_digits(s2, "2", "M");

    // 处理顺子
    if(s1.find("10") < s1.size())
        s1.replace(s1.find("10"), strlen("10"), ":");
    else if(s2.find("10") < s2.size())
        s2.replace(s2.find("10"), strlen("10"), ":");

    if(abs(int(s1.size() - s2.size())) >= 2)
    {
        cout << "ERROR" << endl;
        return 0;
    }

    if(s1 > s2)
        cout << s1_tmp << endl;
    else
        cout << s2_tmp << endl;    

    return 0;
}
*/



// 下面是老师讲解
string FindMax(const string& line)
{
    // 如果存在大小王
    if(line.find("joker JOKER") != string::npos)
        return "joker JOKER";

    // 分割两手牌
    int dash = line.find('-');
    string card1 = line.substr(0, dash);
    string card2 = line.substr(dash + 1);

    // 获取两手牌各自张数
    int card1_cnt = count(card1.begin(), card1.end(), ' ') + 1;
    int card2_cnt = count(card2.begin(), card2.end(), ' ') + 1;

    // 获取两手牌的各自第一张
    string card1_first = card1.substr(0, card1.find(' '));
    string card2_first = card2.substr(0, card2.find(' '));

    // 同类型的牌进行比较
    if(card1_cnt == card2_cnt)
    {
        string com_str = "345678910JQKA2";
        if(com_str.find(card1_first) > com_str.find(card2_first))
            return card1;
        else
            return card2;
    }

    // 处理一方是炸弹，一方不是
    if(card1_cnt == 4)
        return card1;
    else if(card2_cnt == 4)
        return card2;
    else
        return "ERROR";

}

int main()
{
    string line;
    while(getline(cin, line))
    {
        cout << FindMax(line) << endl;
    }
    return 0;
}













